﻿<UserControl 
    x:Class="Perspective.View.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:localVM="clr-namespace:Perspective.ViewModel"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    xmlns:swn="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    xmlns:swct="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
    xmlns:ph="clr-namespace:Perspective.Hosting;assembly=Perspective.Hosting"
    Loaded="UserControl_Loaded"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <localVM:ExtensionViewModel x:Key="ExtensionViewModel"/>
        <CollectionViewSource 
            x:Key="ExtensionsView"
            Source="{Binding Source={StaticResource ExtensionViewModel}, Path=ExtensionLinks}" />
        <CollectionViewSource 
            x:Key="PageLinksView"
            Source="{Binding Source={StaticResource ExtensionViewModel}, Path=CurrentExtension.PageLinks}" />

        <ph:ExtensionContentLoader x:Key="ExtensionContentLoader" />

        <ControlTemplate 
            x:Key="ListBoxTemplate"
            TargetType="ItemsControl">
            <ScrollViewer 
                HorizontalScrollBarVisibility="Disabled"
                VerticalScrollBarVisibility="Auto">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>

        <ItemsPanelTemplate x:Key="ExtensionsPanelTemplate">
            <StackPanel 
                Orientation="Vertical"/>
        </ItemsPanelTemplate>

        <ItemsPanelTemplate x:Key="PageLinksPanelTemplate">
            <swct:WrapPanel />
        </ItemsPanelTemplate>

        <Style x:Key="RectangleStyle" TargetType="Rectangle">
            <Setter Property="RadiusX" Value="8" />
            <Setter Property="RadiusY" Value="8" />
        </Style>

        <Style x:Key="DesktopButtonStyle" TargetType="Button">
            <Setter Property="Padding" Value="10" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Background" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup Name="CommonStates">
                                    <VisualState Name="Normal"/>
                                    <VisualState Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames 
                                                    Storyboard.TargetName="backgroundRectangle" 
                                                    Storyboard.TargetProperty="Opacity" 
                                                    Duration="0">
                                                <DiscreteDoubleKeyFrame KeyTime="0" Value="0.55"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup Name="FocusStates">
                                    <VisualState Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames 
                                                    Storyboard.TargetName="focusRectangle" 
                                                    Storyboard.TargetProperty="Visibility" 
                                                    Duration="0">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames 
                                                    Storyboard.TargetName="backgroundRectangle" 
                                                    Storyboard.TargetProperty="Opacity" 
                                                    Duration="0">
                                                <DiscreteDoubleKeyFrame KeyTime="0" Value="0.55"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Rectangle 
                                Name="backgroundRectangle"
                                Opacity="0.4"
                                Style="{StaticResource RectangleStyle}"
                                Fill="{TemplateBinding Background}"
                                />
                            <ContentPresenter
                                Content="{TemplateBinding Content}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Padding}"
                                />
                            <Rectangle 
                                Name="focusRectangle"
                                Opacity="0.7"
                                Style="{StaticResource RectangleStyle}"
                                Visibility="Collapsed"
                                Stroke="White"
                                StrokeThickness="3"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style 
            TargetType="TextBlock">
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Margin" Value="0,2,0,0" />
        </Style>

        <DataTemplate x:Key="ExtensionTemplate">
            <Button 
                Style="{StaticResource DesktopButtonStyle}"
                Command="{Binding 
                    Source={StaticResource ExtensionViewModel}, 
                    Path=SetCurrentExtensionCommand}"
                CommandParameter="{Binding}">
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Image 
                        Grid.Row="0"
                        Source="{Binding Icon}"/>
                    <Border 
                        Grid.Row="1"
                        CornerRadius="4"
                        Margin="0, 2, 0, 0"
                        Padding="5, 0, 5, 1">
                        <Border.Background>
                            <SolidColorBrush Color="White" Opacity="0.8" />
                        </Border.Background>
                        <TextBlock
                            HorizontalAlignment="Center"
                            TextTrimming="WordEllipsis"
                            Text="{Binding Path=Title}"/>
                    </Border>
                </Grid>
            </Button>
        </DataTemplate>

        <DataTemplate x:Key="PageLinkTemplate">
            <Button 
                Style="{StaticResource DesktopButtonStyle}"
                Command="{Binding 
                    Source={StaticResource ExtensionViewModel}, 
                    Path=SetCurrentPageLinkCommand}"
                CommandParameter="{Binding}">
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Image 
                        Grid.Row="0"
                        Source="{Binding Icon}"/>
                    <Border 
                        Grid.Row="1"
                        CornerRadius="4"
                        Margin="0, 2, 0, 0"
                        Padding="5, 0, 5, 1">
                        <Border.Background>
                            <SolidColorBrush Color="White" Opacity="0.8" />
                        </Border.Background>
                        <TextBlock
                            HorizontalAlignment="Center"
                            TextTrimming="WordEllipsis"
                            Text="{Binding Path=Title}"/>
                    </Border>
                </Grid>
            </Button>
        </DataTemplate>

        <Style 
            x:Key="ItemContainerStyle"
            TargetType="ListBoxItem">
            <Setter Property="Width" Value="160"/>
            <Setter Property="Height" Value="120"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <ContentPresenter 
                            Margin="5"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--<swn:UriMapper x:Key="UriMapper">
             1) Mapping neutre pour ne pas déclencher mapping 3) si URI complet 
            <swn:UriMapping 
                Uri="/{assembly};component/View/{pageName}.xaml" 
                MappedUri="/{assembly};component/View/{pageName}.xaml"/>
             2) Mapping pour passage d'arguments
            <swn:UriMapping 
                Uri="/{assembly}/{pageName}/{argKey}/{argValue}" 
                MappedUri="/{assembly};component/View/{pageName}.xaml?{argKey}={argValue}"/>
             3) Mapping pour URI simplifiée
            <swn:UriMapping 
                Uri="/{assembly}/{pageName}" 
                MappedUri="/{assembly};component/View/{pageName}.xaml"/>
        </swn:UriMapper>-->
        
        <ph:ExtensionUriMapper x:Key="UriMapper" />
        
    </UserControl.Resources>
    <Grid 
        Background="#EBB462">
        <swct:DockPanel>
            <ListBox 
                Name="extensionListBox"
                Margin="10"
                swct:DockPanel.Dock="Left"          
                ItemsSource="{Binding Source={StaticResource ExtensionsView}}"
                Template="{StaticResource ListBoxTemplate}"
                ItemsPanel="{StaticResource ExtensionsPanelTemplate}"
                ItemTemplate="{StaticResource ExtensionTemplate}"
                ItemContainerStyle="{StaticResource ItemContainerStyle}"
                GotFocus="extensionListBox_GotFocus"/>
            <!--<TextBlock 
                swct:DockPanel.Dock="Bottom"
                Text="{Binding Source={StaticResource ExtensionViewModel}, Path=CurrentExtension}"/>-->
            <Grid 
                Name="detailGrid">
                <ListBox 
                    Name="detailListBox"
                    Margin="10"
                    swct:DockPanel.Dock="Left"          
                    ItemsSource="{Binding Source={StaticResource PageLinksView}}"
                    Template="{StaticResource ListBoxTemplate}"
                    ItemsPanel="{StaticResource PageLinksPanelTemplate}"
                    ItemTemplate="{StaticResource PageLinkTemplate}"
                    ItemContainerStyle="{StaticResource ItemContainerStyle}" />
                <!--We could use binding here, but it would be broken by fragment navigation...-->
                <!--Source="{Binding Source={StaticResource ExtensionViewModel}, Path=CurrentPageLink.Uri}"-->
                <navigation:Frame 
                    Name="frame" 
                    UriMapper="{StaticResource UriMapper}"
                    ContentLoader="{StaticResource ExtensionContentLoader}"
                    Background="White"
                    Navigated="frame_Navigated"
                    Visibility="Collapsed"/>
                <!--UriMapper="{StaticResource UriMapper}"-->
            </Grid>
        </swct:DockPanel>
    </Grid>
</UserControl>
